﻿///<reference path="../../scripts/jquery-1.4.4.min.js" />
///<reference path="../../scripts/jQueryUI/jquery.ui.dialog.js" />
///<reference path="../../scripts/kooboo/kooboo.form.js" />
///<reference path="../../scripts/kooboo/kooboo.birthdate.js" />
///<reference path="../../scripts/kooboo/kooboo.mvcvalidation.js" />



$(function () {

    $("#Birthday").birthdate();

    $("#customer_add_form").submit(function (e) {
        e.preventDefault();
        $(this).ajaxPost({
            success: function (id) {
                $(".search-form").find(".customer_search_keyword").val(id);
                $("#search-customers-form").submit();
                $("#customer-add-dialog").dialog("close");
            },
            box: $(".customer-form")
        });
        return false;
    });

    $("#customer-add-link").click(function (e) {
        $("#customer_add_form").cleanError().find("input:first").focus();
    });

    $("#search-customers-form").submit(function (e) {

        setCustomerId("");
        e.preventDefault();
        $(this).ajaxGet({
            success: function (data) {
                RenderCustomers(data);

                if (data.length == 1) {
                    $("#customer-container > tr >td >input:first").attr("checked", true);
                    setCustomerId(data[0].Id);
                }
            }
        });

        return false;
    });

    $(".checked-customer").live("change", function (e) {
        $(".checked-customer").attr("checked", false);
        $(this).attr("checked", true);

        var customerid = parseInt($(this).parent().next().html());
        setCustomerId(customerid);
    });

    function setCustomerId(id) {
        $("#customer-id").val(id);
        BindAddressbookSelector(id);

        if (id == "") {
            //$("#next_to_product").hide();
            $("#next_to_product").attr("disabled", "disabled");
        } else {
            //$("#next_to_product").show();
            $("#next_to_product").removeAttr("disabled");
        }
    }

    $(".search-form").find("input:first").focus();
    $("#search-customers-form").submit();
});

var Customers = [];
function RenderCustomers(data) {

    EnableCalculator();
    this.Customers = data;
    var container = $("#customer-container");
    container.empty();

    var even = true;

    for (var i = 0; i < this.Customers.length; i++) {
        even = !even;

        var item = this.Customers[i];

        var tempate = $("<tr>"
                                   + "<td><input type='checkbox' class='checked-customer' /></td>"
                                   + "<td></td>"
                                   + "<td></td>"
                                   + "<td></td>"
                                   + "<td></td>"
                                   + "<td></td>"
                                   + "<td></td>"
                                + "</tr>");
        tempate.find("td:eq(1)").html(item.Id);
        tempate.find("td:eq(2)").html(item.Name);
        tempate.find("td:eq(3)").html(item.Mail);
        tempate.find("td:eq(4)").html(item.Telephone);
        tempate.find("td:eq(5)").html(item.Country?item.Country.CountryName:"");
        tempate.find("td:eq(6)").html(item.City);

        if (even) {
            tempate.addClass("even");
        }

        container.append(tempate);
    }
}

function BindAddressbookSelector(customerid) {

    var selectors = $(".addressbook-selector");
    selectors.empty();
    selectors.append($("<option></option>"))
    for (var i = 0; i < this.Customers.length; i++) {
        var find = this.Customers[i];
        if (find.Id == customerid) {
            for (var n = 0; n < find.AddressBooks.length; n++) {
                var book = find.AddressBooks[n];
                selectors.append($("<option></option>").html(book.Name).val(book.Id));
            }
            break;
        }
    }

    EnableCalculator();
}

